﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace SaturniLibrary.Web
{
    /// <summary>
    /// 静态扩展WebViewPage类
    /// </summary>
    public static class WebViewExtensions
    {
        public static void Meta(this WebViewPage webViewPage, string key, string value)
        {

        }

        public static void JS(this WebViewPage webViewPage, string file)
        {
            Dictionary<string, List<string>> scripts;
            if (webViewPage.ViewBag.Scripts == null)
            {
                webViewPage.ViewBag.Scripts = new Dictionary<string, List<string>>();
            }
            scripts = webViewPage.ViewBag.Scripts;
            var path = file.ToLower();

            var from = webViewPage.VirtualPath;

            if (!string.IsNullOrEmpty(path))
            {
                if (!scripts.ContainsKey(from)) scripts.Add(from, new List<string>());
                if (scripts[from].Any(s => s.Equals(path, StringComparison.OrdinalIgnoreCase))) return;
                scripts[from].Add(path);
            }
        }

        public static void CSS(this WebViewPage webViewPage, string file)
        {
            Dictionary<string, List<string>> styles;
            if (webViewPage.ViewBag.Styles == null)
            {
                webViewPage.ViewBag.Styles = new Dictionary<string, List<string>>();
            }
            styles = webViewPage.ViewBag.Styles;

            var from = webViewPage.VirtualPath;

            if (!styles.ContainsKey(from)) styles.Add(from, new List<string>());
            string path = file.ToLower();
            if (styles[from].Any(t => t.Equals(path, StringComparison.OrdinalIgnoreCase))) return;
            styles[from].Add(path);
        }

        public static MvcHtmlString RenderHeader(this WebViewPage webViewPage)
        {
            StringBuilder sb = new StringBuilder();
            if (webViewPage.ViewBag.Styles != null)
            {
                var renderedItems = new List<string>();
                Dictionary<string, List<string>> styles = webViewPage.ViewBag.Styles;

                for (int i = styles.Count - 1; i >= 0; i--)
                {
                    foreach (var style in styles[styles.Keys.ToArray()[i]])
                    {
                        if (renderedItems.Contains(style.ToLower())) continue;

                        sb.AppendFormat("<link rel=\"stylesheet\" style=\"text/css\" href=\"{0}\" />\n", style);

                        renderedItems.Add(style.ToLower());
                    }
                }
            }

            if (webViewPage.ViewBag.Scripts != null)
            {
                var renderedItems = new List<string>();
                Dictionary<string, List<string>> scripts = webViewPage.ViewBag.Scripts;

                for (int i = scripts.Count - 1; i >= 0; i--)
                {
                    foreach (var script in scripts[scripts.Keys.ToArray()[i]])
                    {
                        if (renderedItems.Contains(script.ToLower())) continue;
                        sb.AppendFormat("<script src=\"{0}\" type=\"text/javascript\"></script>\n", script);
                        renderedItems.Add(script.ToLower());
                    }
                }
            }
            return MvcHtmlString.Create(sb.ToString());
        }
    }
}
